home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / Array_c.m4 < prev    next >
Text File  |  1990-05-19  |  4KB  |  196 lines

  1. /* Array_c.m4 -- template for implementations of generic array objects
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     September, 1985
  21.  
  22. Function:
  23.  
  24. m4 macro template for the .c files for arrays of the specified
  25. fundamental type: char, int, short, long, unsigned, float, and double.
  26. For example, to generate the implementation of an array of chars:
  27.  
  28.     m4 Array.c.m4 Arraychar.p >Arraychar.c
  29.  
  30. The type-specific part of the implementation is in the file
  31. Arraychar.p, for example.
  32.  
  33. WARNING -- Make changes to the .p or .m4 files, not to the .c file
  34. they generate.
  35.  
  36. $Log:    Array_c.m4,v $
  37. Revision 3.0  90/05/20  00:18:57  kgorlen
  38. Release for 1st edition.
  39.  
  40. */
  41.  
  42. `#define' NEW(type,size)    ((type*)malloc(sizeof(type)*(size)))
  43.  
  44. define(ARRAYIMPLEMENT,
  45. $1::$1(unsigned size)
  46. {
  47.     sz = size;
  48.     if (size==0) AllocSizeErr();
  49.     v = NEW($2,sz);
  50.     removeAll();
  51. }
  52.  
  53. $1::$1(const $1& a)
  54. {
  55.     register unsigned i = a.sz;
  56.     sz = i;
  57.     v = NEW($2,i);
  58.     register $2* vv = v;
  59.     register $2* av = a.v;
  60.     while (i--) *vv++ = *av++;
  61. }
  62.  
  63. $1::~$1()    { DELETE(v); }
  64.  
  65. void $1::operator=(const $1& a)
  66. {
  67.     if (v != a.v) {
  68.         DELETE(v);
  69.         v = NEW($2,sz=a.sz);
  70.         register unsigned i = a.sz;
  71.         register $2* vv = v;
  72.         register $2* av = a.v;
  73.         while (i--) *vv++ = *av++;
  74.     }
  75. }
  76.  
  77. bool $1::operator==(const $1& a) const
  78. {
  79.     if (sz != a.sz) return NO;
  80.     register unsigned i = sz;
  81.     register $2* vv = v;
  82.     register $2* av = a.v;
  83.     while (i--) if (*vv++ != *av++) return NO;
  84.     return YES;
  85. }
  86.  
  87. unsigned $1::capacity()    const { return sz; }
  88.  
  89. int $1::compare(const Object& p) const
  90. {
  91.     assertArgSpecies(p,classDesc,"compare");
  92.     const $1& a = castdown(p);
  93.     register unsigned i = MIN(sz,a.sz);
  94.     register $2* vv = v;
  95.     register $2* av = a.v;
  96.     while (i--) {
  97.         if (*vv != *av) {
  98.             if (*vv > *av) return 1;
  99.             return -1;
  100.         }
  101.     }
  102.     if (sz > a.sz) return 1;
  103.     if (sz == a.sz) return 0;
  104.     return -1;
  105. }
  106.  
  107. void $1::dumpOn(ostream& strm) const
  108. {
  109.     strm << className() << '[';
  110.     printOn(strm);
  111.     strm << "]\n";
  112. }
  113.  
  114. bool $1::isEqual(const Object& a) const
  115. {
  116.     return a.isSpecies(classDesc) && *this==castdown(a);
  117. }
  118.  
  119. void $1::removeAll()
  120. {
  121.     register unsigned i = sz;
  122.     register $2* vv = v;
  123.     while (i--) *vv++ = 0;
  124. }
  125.  
  126. const Class* $1::species() const { return $1::desc(); }
  127.  
  128. void $1::reSize(unsigned newsz)
  129. {
  130.     if (newsz<=0) AllocSizeErr();
  131.     v = REALLOC(v,newsz);
  132.     sz = newsz;
  133. }
  134.  
  135. void $1::deepenShallowCopy()
  136. {
  137.     BASE::deepenShallowCopy();
  138. }
  139.  
  140. unsigned $1::size() const    { return sz; }
  141.  
  142. void $1::sort()
  143. {
  144.     qsort(v,sz,sizeof($2),$2Cmp);
  145. }
  146.  
  147. extern const int NIHCL_ALLOCSIZE;
  148. extern const int NIHCL_INDEXRANGE;
  149.  
  150. void $1::AllocSizeErr() const
  151. {
  152.     setError(NIHCL_ALLOCSIZE,DEFAULT,this,className());
  153. }
  154.  
  155. void $1::IndexRangeErr() const
  156. {
  157.     setError(NIHCL_INDEXRANGE,DEFAULT,this,className());
  158. }
  159.  
  160. Object* $1::add(Object&)
  161. {
  162.     shouldNotImplement("add");
  163.     return 0;
  164. }
  165.  
  166. Object*& $1::at(int)
  167. {
  168.     shouldNotImplement("at");
  169.     return (Object*&)nil;
  170. }
  171.  
  172. const Object *const& $1::at(int) const
  173. {
  174.     shouldNotImplement("at");
  175.     return (Object *const&)nil;
  176. }
  177.  
  178. Object* $1::doNext(Iterator&) const
  179. {
  180.     shouldNotImplement("doNext");
  181.     return 0;
  182. }
  183.  
  184. unsigned $1::occurrencesOf(const Object&) const
  185. {
  186.     shouldNotImplement("occurrencesOf");
  187.     return 0;
  188. }
  189.  
  190. Object* $1::remove(const Object&)
  191. {
  192.     shouldNotImplement("remove");
  193.     return 0;
  194. }
  195. )
  196.